var page = 1;
var pagenum = 10;
var fi = 1
var url = {
	role_list: api.role.list,
	add: api.user.add,
	edit: api.user.edit,
	list:  api.user.list,
}
upImage( '{:url("/adminapi/Upload/uploadImg")}','#test1', '#img1',data)
ajax(url.role_list, {page:1,pagenum:1000}, 'post').done(function(res) {
	
	console.log(res)
	var options=`<option value='0'>全部角色</option>`;
	for(var i=0;i<res.list.length;i++){
		{if $Think.get.role_id}
		if(res.list[i].role_id=="{$Think.get.role_id}"){
			options+=`<option selected value='${res.list[i].role_id}'>${res.list[i].role_name}</option>`
		}else{
			options+=`<option value='${res.list[i].role_id}'>${res.list[i].role_name}</option>`
		}
		
		{else/}
		options+=`<option value='${res.list[i].role_id}'>${res.list[i].role_name}</option>`
		{/if}
	}
	$('select[name="role_id"]').html(options)
	form.render('select')
	
}).fail(function(err) {
	Err('请求失败')
});

var main = [
	[ //表头
	{if $Think.get.op}
			
		{
			title: '',
			toolbar: '#choose',
			fixed: 'left'
		},
		{else/}
		{/if}
		 {
			field: 'username',
			title: '账号',
			sort: true,
			fixed: 'left'
		}, {
			field: 'role_name',
			title: '角色',
			sort: true
		}, {
			field: 'mobile',
			width: 200,
			title: '手机号'
		}, {
			field: 'state2',
			width: 200,
			title: '状态'
		}, 
		{if $Think.get.op}
		{else/}
		{
			title: '操作',
			width: 250,
			toolbar: '#operation',
			fixed: 'right'
		}
		{/if}
	]
];
var data = {
	page:1,
	pagenum:10
};
var where_json={}
{if $Think.get.user_name}
	where_json.username=['like','%{$Think.get.user_name}%']
{/if}
{if $Think.get.role_id}
	where_json.role_id=['in','{$Think.get.role_id}']
{/if}
data.where_json=JSON.stringify(where_json)
ajaxdriver(data)

function ajaxdriver(data) {
	console.log(data)
	Load()
	ajax(url.list, data, 'post').done(function(res) {
		Load(1)
		if(res.code == 1) {
			console.log(res)
			for(var i=0;i<res.list.length;i++){
				if(res.list[i].state==1){
					res.list[i].state2='启用'
				}else{
					res.list[i].state2='禁用'
				}
				var role_name='';
				for(var ii=0;ii<res.list[i].roles.length;ii++){
					if(ii==0){
						role_name+=res.list[i].roles[ii].role_name
					}else{
						role_name+=(','+res.list[i].roles[ii].role_name)
					}
				}
				res.list[i].role_name=role_name
			}
			table.render({
				elem: '#driver',
				cellMinWidth: 80,
				cols: main,
				data: res.list,
				limit: data.pagenum,
				even: true
			});
			if(fi==1){
				 laypage.render({
				    elem: 'page'
				    ,count: res.count
					,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
					,theme: '#1E9FFF'
				    ,jump: function(obj, first){
				    	if(!first){
				       		page=obj.curr
	    					data.page=page
	    					pagenum=obj.limit
	    					data.pagenum=pagenum
	    					ajaxdriver(data)
    					}else{
    						
    					}
				    }
				  });
				  fi=2
				 }
			{if $Think.get.op}
			table.on('tool(driver)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				if(layEvent === 'choose') {
					var index = parent.layer.getFrameIndex(window.name);
					console.log('选中了')
					parent.$('.user_id').val(data1.user_id)
					parent.$('.username').val(data1.username)
					parent.$('.phone').val(data1.mobile)
					parent.layer.close(index);
				}
			})
			{else/}
			table.on('tool(driver)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				if(layEvent === 'delete') { //删除
					layer.confirm('是否删除', function(index) {
						Load();
						ajax(api.user.delete,{
							user_ids: data1.user_id
						},'post').done(function(res) {
							if(res.code == 1) {
									ajaxdriver(data)
									layer.close(index);
								} else {
									layer.close(index);
									Load(1)
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
						}).fail(function(err) {
							Load(1)
							Err('请求失败')
						});
						
						layer.close(index);
					});
				} else if(layEvent === 'edit') { //编辑
					//do something
					window.location.href = " {:url('/html/system/user_add')}?id=" + data1.user_id
					//同步更新缓存对应的值

				}  else if(layEvent === 'forbidden') {
					layer.confirm('是否禁用', function(index) {
						Load()
						data1.state=9
						ajax(api.user.edit,data1,'post').done(function(res) {
							if(res.code == 1) {
								ajaxdriver(data)
								layer.close(index);
							} else {
								layer.close(index);
								Load(1)
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}
						}).fail(function(err) {
							Load(1)
							Err('请求失败')
						});
						layer.close(index);
					});
				} else if(layEvent === 'startusing') {
					layer.confirm('是否启用', function(index) {
						Load()
						data1.state=1
						ajax(api.user.edit,data1,'post').done(function(res) {
							if(res.code == 1) {
								ajaxdriver(data)
								layer.close(index);
							} else {
								layer.close(index);
								Load(1)
								Err(res.msg ? res.msg : '操作失败，稍候再试');
							}
						}).fail(function(err) {
							Load(1)
							Err('请求失败')
						});
						
						layer.close(index);
					});
				}
			});
			{/if}
			//删除多个

			$(".delete-multiple").on('click', function() {
				var driver_ids = '';
				var checkStatus = table.checkStatus('driver'); //test即为基础参数id对应的值
				console.log(checkStatus.data) //获取选中行的数据
				console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
				console.log(checkStatus.isAll) //表格是否全选
				for(var dei = 0; dei < checkStatus.data.length; dei++) {
					if(dei == 0) {
						driver_ids += checkStatus.data[dei].driver_id
					} else {
						driver_ids += ("," + checkStatus.data[dei].driver_id)
					}
				}
				console.log(driver_ids)
				layer.open({
					type: 1,
					title: false,
					id: "delete",
					content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
					btn: ['确定', '取消'],
					btnAlign: 'c' //按钮居中
						,
					shade: 0 //不显示遮罩
						,
					yes: function() {
						var deleteload = layer.load(1, {
							shade: [0.1, '#fff'] //0.1透明度的白色背景
						});
						ajax(url.delete,{
								driver_ids: driver_ids
							},data,'post').done(function(res) {
							console.log(res)
								if(res.code == 1) {
									layer.close(deleteload);
									layer.closeAll();
									Msg('删除成功')
									ajaxdriver(data)
								} else {
									layer.close(deleteload);
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
						}).fail(function(err) {
							Load(1)
							Err('请求失败')
						});
						
						

					},
					btn2: function() {
						layer.closeAll();
					}
				});
			});

		} else {
			Err(res.msg)
		}
	}).fail(function(err) {
		Load(1)
		Err('请求失败')
	});

}

//添加
$('.add').on('click', function() {
	{if $Think.get.op}
	window.location.href = '{:url("/html/system/user_add")}?op=search'
	{else/}
	window.location.href = '{:url("/html/system/user_add")}'
	{/if}
});

